package com.tongqin.distributed;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {

        //连接工厂
        ConnectionFactory connectionFactory =
                new ActiveMQConnectionFactory("tcp://172.16.233.128:61616");
        Connection connection = null;
        try {
            //通过连接工厂创建连接
             connection = connectionFactory.createConnection();
             connection.setClientID("change-001");
             //开启连接一定要这个
             connection.start();
            //两台机器连接之后建立起会话
            Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);

//            Session.auto_acknowledge
//            Session.client_acknowledge:
//            Session.dups_ok_acknowledge: 延迟确认


            //消息中间件的管理中心queue，消息队列，可以保存多个消息
            Destination destination = session.createQueue("myqueue");


            //会话也是两个端，一个生产端producer一个消费端consumer
            MessageProducer producer = session.createProducer(destination);
//            producer.setDeliveryMode();
            //会话创建需要发送的信息内容
            TextMessage textMessage = session.createTextMessage();
            textMessage.setText("hello world");
            //手动签收
//            textMessage.acknowledge();
            //发送
//            producer.setDeliveryMode();
            producer.send(textMessage);
            //提交在关闭 是连接一般都需要关闭
            session.commit();
            session.rollback();
            session.close();


        } catch (JMSException e) {
            e.printStackTrace();
        }finally {
            if(connection != null){
                try {
                    connection.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }

        System.out.println("什么鬼");
    }
}
